<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{active:isShow}">
      <span v-if="!fullLocation" class="placeholder">请选择配送地址</span>
      <span v-else class="value">{{ fullLocation }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show='isShow'>
      <template v-if='cityList.length'>
         <span @click="changeCity(item)" class="ellipsis" v-for="item in clist" :key="item.code">{{item.name}}</span>
      </template>
      <div v-else class="loading"></div>
    </div>
  </div>
</template>
<script>
import { ref, reactive, computed } from 'vue'
import { onClickOutside } from '@vueuse/core'
import { getCityListAPI } from '@/api/product.js'
export default {
  name: 'XtxCity',
  emits: ['change-city'],
  props: {
    fullLocation: {
      type: String,
      default: ''
    }
  },
  setup (props, { emit }) {
    // 选中的省市区
    const changeResult = reactive({
      provinceCode: '',
      provinceName: '',
      cityCode: '',
      cityName: '',
      countyCode: '',
      countyName: '',
      fullLocation: ''
    })
    const changeCity = (cityInfo) => {
      if (cityInfo.level === 0) {
        changeResult.provinceCode = cityInfo.code
        changeResult.provinceName = cityInfo.name
      } else if (cityInfo.level === 1) {
        changeResult.cityCode = cityInfo.code
        changeResult.cityName = cityInfo.name
      } else {
        changeResult.countyCode = cityInfo.code
        changeResult.countyName = cityInfo.name
        changeResult.fullLocation = `${changeResult.provinceName} ${changeResult.cityName} ${changeResult.countyName}`

        emit('change-city', changeResult)
        // = 关闭弹层
        isShow.value = false
      }
    }

    // 计算省市区类别数据
    const clist = computed(() => {
      // 省级列表
      let result = cityList.value
      // 计算市级列表
      if (changeResult.provinceCode && changeResult.provinceName) {
        // 省级单位已经选中，可以计算市级单位
        result = result.find(item => item.code === changeResult.provinceCode).areaList
      }
      if (changeResult.cityCode && changeResult.cityName) {
        // 选中了市级单位，此时可以获取县级列表
        result = result.find(item => item.code === changeResult.cityCode).areaList
      }
      return result
    })

    const target = ref(null)
    onClickOutside(target, () => { isShow.value = false })

    const isShow = ref(false)
    const cityList = ref([])
    // 控制选择城市弹窗的显示和隐藏
    const toggle = () => {
      isShow.value = !isShow.value
      if (isShow.value) {
        getCityListAPI().then(data => {
          cityList.value = data
        })
      }
      for (const key in changeResult) {
        changeResult[key] = ''
      }
    }
    return { isShow, toggle, target, cityList, changeCity, changeResult, clist }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .option {
    // 省略...
    .loading {
      height: 290px;
      width: 100%;
      background: url(../../assets/images/loading.gif) no-repeat center;
    }
}
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
